package com.huluspace.authsecurity.controller;

import com.huluspace.authsecurity.dto.LoginRequest;
import com.huluspace.authsecurity.dto.LoginResponse;
import com.huluspace.authsecurity.security.JwtUtil;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
public class AuthController {

  // 注入JwtUtil
  private final JwtUtil jwtUtil;
  public AuthController(JwtUtil jwtUtil) {
    this.jwtUtil = jwtUtil;
  }

  @PostMapping("/login")
  public LoginResponse login(@RequestBody LoginRequest request) {
    // 若用户名和密码正确则生产令牌返回
    if ("admin".equals(request.username) && "123456".equals(request.password)) {
      String token = jwtUtil.generateToken(request.username);
      return new LoginResponse(token);
    } else {
      throw new RuntimeException("用户名或密码错误");
    }
  }
}

